//
//  ClubViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/7.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ClubViewController.h"
#import "ClubHeaderView.h"
#import "ClubTableViewCell.h"
#import "ShopTableViewCell.h"
#import "ShopInfoViewController.h"
#import "ClubModel.h"
#import "ShopModel.h"

@interface ClubViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, copy)NSString * type;
@property (nonatomic, strong)NSArray <ClubModel*> * modelListArray;
@property (nonatomic, strong)NSArray <ShopModel*> * shopListArray;

@end

@implementation ClubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view .backgroundColor = WHITE_COLOR;
    
    self.type = @"one";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeTableViewData:) name:@"ChangeTableViewData" object:nil];
    
    [self loadDataWithType:self.type];
    [self setupUI];
}


- (void)loadDataWithType:(NSString *)type{
    
    NSString * str;
    
    if ([type isEqualToString:@"one"]) {
        
        str = @"33";
        [ClubModel clubModelWithType:str WithSucess:^(NSArray<ClubModel *> *array) {
            
            self.modelListArray = array;
        } error:^{
            
        }];
    }else if([type isEqualToString:@"two"]){
        
        str = @"34";
        [ClubModel clubModelWithType:str WithSucess:^(NSArray<ClubModel *> *array) {
            
            self.modelListArray = array;
        } error:^{
            
        }];
    }else if ([type isEqualToString:@"three"]){
        
        [ShopModel shopModelWithSucess:^(NSArray<ShopModel *> *array) {
            
            self.shopListArray = array;
        } error:^{
            
        }];
        
    }
    
    
}

- (void)setModelListArray:(NSArray<ClubModel *> *)modelListArray{
    
    _modelListArray = modelListArray;
    
    [self.tableView reloadData];
}

- (void)setShopListArray:(NSArray<ShopModel *> *)shopListArray{
    
    _shopListArray = shopListArray;
    
    [self.tableView reloadData];
}

- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 80) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    
    [self.tableView registerClass:[ClubTableViewCell class] forCellReuseIdentifier:@"ClubListCell"];
    
    [self.tableView registerClass:[ShopTableViewCell class] forCellReuseIdentifier:@"ShopListCell"];
    
    [self.tableView registerClass:[ClubHeaderView class] forHeaderFooterViewReuseIdentifier:@"ClubHeaderView"];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([self.type isEqualToString:@"one"] || [self.type isEqualToString:@"two"]) {
        
        return self.modelListArray.count;
    }else if([self.type isEqualToString:@"three"]){
        
        return self.shopListArray.count;
    }else{
        
        return 7;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.type isEqualToString:@"one"]  || [self.type isEqualToString:@"two"]) {
        
        ClubTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClubListCell" forIndexPath:indexPath];
        
        cell.model = self.modelListArray[indexPath.row];
        
        return cell;
    }else if([self.type isEqualToString:@"three"]){
        
        ShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShopListCell" forIndexPath:indexPath];
        
        cell.model = self.shopListArray[indexPath.row];
        
        return cell;
    }else{
        
        return nil;
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40* SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    ClubHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ClubHeaderView"];
    
    return header;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.type isEqualToString:@"one"] || [self.type isEqualToString:@"two"]) {
        
        return 261.5 * SCREENH6;
    }else{
        
        return 83 * SCREENH6;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    if([self.type isEqualToString:@"three"]){
        
        ShopModel * model = self.shopListArray[indexPath.row];
        
        ShopInfoViewController * infoVC = [ShopInfoViewController new];
        
        infoVC.ID = model.ID;
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }
    
    
    
    SHOW_TAB;
}


- (void)ChangeTableViewData:(NSNotification *)noti{
    
    self.type = noti.userInfo[@"type"];
//
//    NSLog(@"==%@",self.type);
    
    [self loadDataWithType:self.type];
//    [self.tableView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
